<?php
namespace app\controller;
use app\BaseController;
use app\model\Fenlei;
use app\model\Searchkll;
use think\facade\View;

class Caiji extends BaseController{
    //把采集过来的数据入库做进一步处理
    public function chuli(){
        $model=new \app\model\Caiji();
        $find=$model->where('temp',0)->find()->toArray();
        $data=json_decode($find['alljson'],true);
//        p($data);
        $kll=new Searchkll();
        $ins['name']=$data['foodName'];
        $ins['reliang']=$data['foodCaloriesVal'];
        try {
            $kll->insert($ins);


        $updata=$model->where('id',$find['id'])
            ->update(['temp'=>1,'img'=>saveLinkImg($data['foodIcon'])]);
        }catch (\Exception $exception){
            $updata=$model->where('id',$find['id'])
                ->update(['temp'=>1]);
        }
        View::assign('page',$ins['name']);
        return View::fetch();
    }
    //采集分类 已完成
    public function fenlei(){
//        $url = 'https://www.miaofoods.com/miaozsPc/foodHome/getFoodHomeInfo?param=%7B%22cateName%22:%22JUANZISC0G%22,%22range%22:%7B%22page%22:1,%22pageSize%22:100%7D%7D';
//        $url=urldecode($url);
        $url='https://www.miaofoods.com/miaozsPc/foodHome/getFoodHomeInfo?param={"cateName":"0DQM1ZK2G6","range":{"page":0,"pageSize":100}}';
//        p($url);
        $html = file_get_contents($url);
        $html=json_decode($html,true);
        $fenlie=($html['result']['foodCateList']);
//        foreach ($fenlie as $v){
//            $model=new Fenlei();
//            $model->img=saveLinkImg($v['imageUrl']);
//            $model->name=$v['cateName'];
//            $model->token=$v['cateToken'];
//            $model->save();
//        }
        p($html['list']);
        p($html['foodCateList']);
    }
    //采集分页信息 已完成
    public function index($page=0){
//        $url = 'https://www.miaofoods.com/miaozsPc/foodHome/getFoodHomeInfo?param=%7B%22cateName%22:%22JUANZISC0G%22,%22range%22:%7B%22page%22:1,%22pageSize%22:100%7D%7D';
//        $url=urldecode($url);
        $model=new Fenlei();
        $feiLeiName=$model->where('temp',1)
            ->find();
//        p($feiLeiName['token']);
        $token=($feiLeiName['token']);
        $url='https://www.miaofoods.com/miaozsPc/foodHome/getFoodHomeInfo?param={"cateName":"'.$token.'","range":{"page":'.$page.',"pageSize":100}}';
//        p($url);
        $html = file_get_contents($url);
        $html=json_decode($html,true);
        unset($html['result']['hotFoodList']);
        unset($html['result']['friendLinkList']);
        unset($html['result']['foodCateList']);

//        p($html);
//        p($html['result']['total']);
//        $model->total=($html['result']['total']);
//        p($html);
//        p($html['status']);
//        p($html['result']['list']);
        if(isset($html['result']['list'])){
            $html=($html['result']['list']);
        }else{
            $html=[];
        }
        if($html==[]){ //已经循环过所有数据后
            $page=0;
            echo $feiLeiName->temp;
            echo $feiLeiName->id;
            $model->where(['id'=>$feiLeiName->id])
                ->update(['temp'=>2]);
//            die;
        }else{
//            p($html);
            //有数据，则循环写入数据库中
//            $kll=new Searchkll();
            $caiji = new \app\model\Caiji();
            foreach ($html as $v){
//                $data['name']=$v['foodName'];
//                $data['reliang']=$v['foodCaloriesVal'];
//                $kll->insert($data);
                $data2['alljson']=json_encode($v);
//                $data2['img']=saveLinkImg($v['foodIcon']);
                $caiji->insert($data2);
            }
            $page+=1;
        }


//        p($html);
//        p($html['foodCateList']);
        View::assign('page',$page);
        return View::fetch();
    }


}
